/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package main;

import com.google.code.eventsonfire.Events;
import comunicacao.DBServerConnector;
import comunicacao.EventoElectPrimary;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.zookeeper.AsyncCallback.StatCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
 *
* @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class DataWatcherRunnable implements Runnable, Watcher, StatCallback {
    
    ZooKeeper zk;

    public DataWatcherRunnable(ZooKeeper zk) {
        this.zk = zk;
        
    }


    @Override
    public void run() {
        try {
            zk.register(this);

            zk.exists("/Ready/DBready",this);
            //zk.getChildren("/AppServers", this, null);
           

        } catch (KeeperException ex) {
            Logger.getLogger(DataWatcherRunnable.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(DataWatcherRunnable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void process(WatchedEvent event) {
        try {
            Thread.sleep(4000);
        } catch (InterruptedException ex) {
            Logger.getLogger(DataWatcherRunnable.class.getName()).log(Level.SEVERE, null, ex);
        }
        Events.fire(this, new EventoElectPrimary());
        System.out.println("entrei process");
        
        
       
    }

    @Override
    public void processResult(int rc, String path, Object ctx, Stat stat) {
        Events.fire(this, "entrou processResult");
        System.out.println("entrei processResult");
    }
    
}
